@file:Suppress("unused")

package com.maodou.core.utils

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.view.View.inflate
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.cardview.widget.CardView
import androidx.fragment.app.Fragment
import com.maodou.core.R

private var toast: Toast? = null

private var toastLegacy: Toast? = null

fun Context.toastOnUi(message: Int, duration: Int = Toast.LENGTH_SHORT) {
    toastOnUi(getString(message), duration)
}

@SuppressLint("InflateParams")
@Suppress("DEPRECATION")
fun Context.toastOnUi(
    message: CharSequence?,
    backgroundColor: Int = Color.parseColor("#cc000000"),
    textColor: Int = Color.parseColor("#ffffffff"),
    duration: Int = Toast.LENGTH_SHORT,
    gravity: Int?,
    topIcon: Int? = null
) {
    kotlin.runCatching {
        toast?.cancel()
        toast = Toast(this)
        val toastView: View = inflate(this, R.layout.view_toast, null)
        toast?.view = toastView
        val cardView = toastView.findViewById<CardView>(R.id.cv_content)
        cardView.setCardBackgroundColor(backgroundColor)
        val textView = toastView.findViewById<TextView>(R.id.tv_text)
        val tipsIcon = toastView.findViewById<ImageView>(R.id.tips_icon)
        textView.setTextColor(textColor)
        textView.text = message
        topIcon?.let {
            tipsIcon.setImageResource(it)
            tipsIcon.visibility = View.VISIBLE
            cardView.cardElevation = 0f
        }
        toast?.duration = duration

        gravity?.let { toast?.setGravity(gravity, 0, 0) }

        toast?.show()
    }
}

fun Context.toastOnUi(message: CharSequence?, duration: Int = Toast.LENGTH_SHORT) {
    toastOnCenterUi(message = message, duration = duration)
}

fun Context.toastOnCenterUi(message: CharSequence?, duration: Int = Toast.LENGTH_SHORT) {
    if (message?.contains("Job was cancelled") ?: false){
        return
    }
    toastOnUi(
        message = message,
        backgroundColor = Color.parseColor("#cc000000"),
        textColor = Color.parseColor("#ffffffff"),
        duration = duration,
        gravity = Gravity.CENTER
    )
}

fun Context.toastOnIconCenterUi(message: CharSequence?, icon:Int) {
    toastOnUi(
        message = message,
        backgroundColor = Color.parseColor("#cc000000"),
        textColor = Color.parseColor("#ffffffff"),
        duration = Toast.LENGTH_SHORT,
        gravity = Gravity.CENTER,
        topIcon = icon
    )
}

fun Context.toastOnUiLegacy(message: CharSequence) {
    kotlin.runCatching {
        if (toastLegacy == null) {
            toastLegacy = Toast.makeText(this, message, Toast.LENGTH_SHORT)
        } else {
            toastLegacy?.setText(message)
            toastLegacy?.duration = Toast.LENGTH_SHORT
        }
        toastLegacy?.show()
    }
}

fun Context.longToastOnUi(message: Int) {
    toastOnUi(message, Toast.LENGTH_LONG)
}

fun Context.longToastOnUi(message: CharSequence?) {
    toastOnUi(message, Toast.LENGTH_LONG)
}

fun Context.longToastOnUiLegacy(message: CharSequence) {
    kotlin.runCatching {
        if (toastLegacy == null) {
            toastLegacy = Toast.makeText(this, message, Toast.LENGTH_LONG)
        } else {
            toastLegacy?.setText(message)
            toastLegacy?.duration = Toast.LENGTH_LONG
        }
        toastLegacy?.show()
    }
}

fun Fragment.toastOnUi(message: Int) = requireActivity().toastOnUi(message)

fun Fragment.toastOnUi(message: CharSequence) = requireActivity().toastOnUi(message)

fun Fragment.longToast(message: Int) = requireContext().longToastOnUi(message)

fun Fragment.longToast(message: CharSequence) = requireContext().longToastOnUi(message)
